#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
  pid_t id = fork();
    if(id == 0)
    {
        //子进程
        int cnt = 50;
        while(cnt)
        {
            printf("我是子进程，我还活着呢，我还有%dS, pid: %d, ppid%d\n", cnt--, getpid(), getppid());
            sleep(1);
            //int *p = NULL;
            //*p = 100;
        }
        exit(111);
    }
  // 父进程
    //pid_t ret_id = wait(NULL);
    while(1)
    {
        int status = 0;
        pid_t ret_id = waitpid(id, &status, WNOHANG); // 夯住了
        if(ret_id < 0)
        {
            printf("waitpid error!\n");
            exit(1);
        }
        else if(ret_id == 0)
        {
            RunTask();
            sleep(1);
            continue;
        }
        else{
            if(WIFEXITED(status)) // 是否收到信号
            {
                printf("wait success, child exit code: %d\n", WEXITSTATUS(status));
            }
            else
            {
                printf("wait success, child exit signal: %d\n", status & 0x7F);
            }
            printf("我是父进程，等待子进程成功, pid: %d, ppid: %d, ret_id: %d, child exit code: %d, child exit signal: %d\n",\
                getpid(), getppid(), ret_id, (status>>8)&0xFF, status & 0x7F);
            break;
        }
    }
  //  printf("hello world\n"); // 输出缓冲区
   // sleep(2);
   // exit(107);

}
